package com.jboy.utils;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ObjectMapperUtil {
    /**
     * 序列化
     * @param object
     * @return
     */
    public static String writeValueAsString(Object object) {
        try {
            ObjectMapper objectMapper = ObjectMapperInstance.INSTANCE.getObjectMapper();
            return objectMapper.writeValueAsString(object);
        } catch (Exception e) {
            log.error("ObjectMapperUtil writeValueAsString error", e);
            return null;
        }
    }

    /**
     * 反序列化
     *
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T readValue(String json, Class<T> clazz) {
        try {
            ObjectMapper objectMapper = ObjectMapperInstance.INSTANCE.getObjectMapper();
            return objectMapper.readValue(json, clazz);
        } catch (Exception e) {
            log.error("ObjectMapperUtil readValue error", e);
        }
        return null;
    }

}
